/*
 * @Description: 
 * @Author: chenjiaxin
 * @Date: 2021-12-21 20:00:06
 * @LastEditTime: 2022-01-08 18:56:32
 * @FilePath: \novel-serve\src\app\error-handle.js
 */
const errorTypes = require("../constants/error-types");
const { responseHandle } = require("../utils/tool");

const errorTypeValues = Object.values(errorTypes);

const errorHandle = (errorObj, ctx, errorMsg) => {
  let code, message;
  let { code: err_code, type: err_type, msg: err_msg } = errorObj;
  const result = errorTypeValues.find(ele => ele.type === err_type);
  if (result) {
    code = err_code;
    message = errorMsg || err_msg;
  } else {
    code = 404;
    message = "NOT FOUND ~~"
  }
  // ctx.body = {
  //   code,
  //   data: message
  // };
  ctx.body = responseHandle(code, message);

}

module.exports = errorHandle;